package lightswitch.server.code.sqlhelper.expression.operators.relational;

import java.util.List;

import lightswitch.server.code.object.DataRow;
import lightswitch.server.code.sqlhelper.ExpressionHelper;
import lightswitch.server.code.sqlhelper.expression.Expression;
import lightswitch.server.code.sqlhelper.expression.SingleExpression;
import lightswitch.server.code.sqlhelper.statement.select.From;
import lightswitch.server.code.util.TypeOfData;

public class Between extends Expression{

	public Expression _value;
	public Expression _from;
	public Expression _to;
	public boolean _isNot;
	public Between(String value, String fr, String to,boolean isnot, From from)
	{
		_value = ExpressionHelper.parseExpression(value,from);
		_from = ExpressionHelper.parseExpression(fr,from);
		_to = ExpressionHelper.parseExpression(to,from);
		_isNot = isnot;
	}
	public Between(Expression value,Expression from,Expression to, boolean isnot)
	{
		_value = value;
		_from = from;
		_to = to;
		_isNot = isnot;
	}
	public String toString()
	{
		return "("+_value.toString() + (_isNot==true?" not between ":  " Between " )+ _from.toString() + " and " + _to.toString() +" )";
	}
	public Object GetValue(DataRow row, List<From> parentFromList) {
		dataType = TypeOfData.BOOLEAN;
		Object value = _value.GetValue(row,parentFromList);
		Object from = _from.GetValue(row,parentFromList);
		Object to = _to.GetValue(row,parentFromList);
		boolean res = false;
		String type1 = ((SingleExpression)_value).dataType;
		String type2 = ((SingleExpression)_from).dataType;
		String type3 = ((SingleExpression)_to).dataType;
		


		if((!type1.equals(TypeOfData.INT) && !type1.equals(TypeOfData.FLOAT))
				|| (!type2.equals(TypeOfData.INT) && !type2.equals(TypeOfData.FLOAT))
				|| (!type3.equals(TypeOfData.INT) && !type3.equals(TypeOfData.FLOAT)))
		{
			//throw exception
			// khi du lieu khac kieu int va float
			return null;
			
		}
		
		else
		{
			float v = Float.parseFloat((((SingleExpression)_value).GetValue(row,parentFromList)).toString());
			float f = Float.parseFloat((((SingleExpression)_from).GetValue(row,parentFromList)).toString());
			float t = Float.parseFloat((((SingleExpression)_to).GetValue(row,parentFromList)).toString());
			
			if(v>=f && v<=t)
				res= true;
			else
				res= false;
			return _isNot==false?res:!res;
		}
	}

	
}
